package ashbyutils.database.testing;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * <p>
 * A simple helper for writing junit tests that need a datasource.
 * </p>
 * <p> - - - - - - - - - - - - - - - - -
 * </p>
 * <p>
 * You are welcome to do whatever you want to with this source file provided
 * that you maintain this comment fragment (between the dashed lines). Modify
 * it, change the package name, change the class name ... personal or business
 * use ... sell it, share it ... add a copyright for the portions you add ...
 * </p>
 * <p>
 * My goal in giving this away and maintaining the copyright is to hopefully
 * direct developers back to JavaRanch.
 * </p>
 * <p>
 * The original source can be found at <a
 * href=http://www.javaranch.com>JavaRanch</a>
 * </p>
 * <p> - - - - - - - - - - - - - - - - -
 * </p>
 */
public class JndiUnitTestHelper {
	private static final String SIMPLE_CONTEXT_FACTORY = "ashbyutils.database.testing.SimpleContextFactory"; //$NON-NLS-1$

	private static final String JNDI_NAME_KEY = "jndiunittesthelper.JNDIName"; //$NON-NLS-1$

	private static final String DATABASE_PASSWORD_KEY = "jndiunittesthelper.dbPassword"; //$NON-NLS-1$

	private static final String DATABASE_USERNAME_KEY = "jndiunittesthelper.dbLogin"; //$NON-NLS-1$

	private static final String DATABASE_SERVER_KEY = "jndiunittesthelper.dbServer"; //$NON-NLS-1$

	private static final String DATABASE_DRIVER_KEY = "jndiunittesthelper.dbDriver"; //$NON-NLS-1$

	private static boolean initialized = false;

	private static String jndiName;

	private JndiUnitTestHelper() {
		// private for singleton pattern
	}

	/**
	 * Intializes the pool and sets it in the InitialContext
	 * 
	 * @param fileName
	 *            Name of the properties file
	 * @throws IOException
	 */
	public static void init(String fileName) throws IOException,
			NamingException {
		SimpleDataSource source = new SimpleDataSource();
		FileInputStream fis = new FileInputStream(fileName);
		Properties props = new Properties();
		props.load(fis);
		source.dbDriver = props.getProperty(DATABASE_DRIVER_KEY);
		source.dbServer = props.getProperty(DATABASE_SERVER_KEY);
		source.dbLogin = props.getProperty(DATABASE_USERNAME_KEY);
		source.dbPassword = props.getProperty(DATABASE_PASSWORD_KEY);
		jndiName = props.getProperty(JNDI_NAME_KEY);
		// Set up environment for creating initial context
		Hashtable env = new Hashtable();
		env.put(Context.INITIAL_CONTEXT_FACTORY, SIMPLE_CONTEXT_FACTORY);
		System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
				SIMPLE_CONTEXT_FACTORY);
		Context ctx = new InitialContext(env);
		// Register the data source to JNDI naming service
		ctx.bind(jndiName, source);
		initialized = true;
	}

	/**
	 * determines if the pool was successfully initialized or not.
	 * 
	 * @returns true if the pool was not successfully initialized.
	 */
	public static boolean notInitialized() {
		return !initialized;
	}

	/**
	 * shutdowns down the pool and ends the Thread that DbConnectionBroker
	 * starts.
	 * 
	 * @throws NamingException
	 */
	public static void shutdown() throws NamingException {
		InitialContext ctx = new InitialContext();
		ctx.unbind(jndiName);
		initialized = false;
	}

	/**
	 * Gets the name of the datasource, useful in test because this is
	 * configurable for the tests ran.
	 */
	public static String getJndiName() {
		return jndiName;
	}
}
